#! /usr/bin/env python
# pytovid

__doc__ = \
"""Convert a video into an (S)VCD/DVD-compliant MPEG (to replace the 'tovid'
shell script).
"""

import sys
from libtovid.opts import Option, Usage, parse
from libtovid import encode
from libtovid import log

# List of valid command-line options, with documentation
allowed_options = [\
    # Filenames
    Option('in', 'FILENAME', None,
        """Input video file, in any format.""",
        required=True),
    Option('out', 'NAME', None,
        """Output prefix or name.""",
        required=True),

    # Format options
    Option('format', 'vcd|svcd|dvd|half-dvd|dvd-vcd', 'dvd',
        """Make video compliant with the specified format"""),
    Option('vcd', alias=('format', 'vcd')),
    Option('svcd', alias=('format', 'svcd')),
    Option('dvd', alias=('format', 'dvd')),
    Option('half-dvd', alias=('format', 'half-dvd')),
    Option('dvd-vcd', alias=('format', 'dvd-vcd')),

    # TV system options
    Option('tvsys', 'pal|ntsc|ntscfilm', 'ntsc',
        """Make the video compliant with the specified TV system"""),        
    Option('ntsc', alias=('tvsys', 'ntsc')),
    #Option('ntscfilm', '', False),
    Option('pal', alias=('tvsys', 'pal')),

    # Aspect ratio options
    Option('aspect', 'WIDTH:HEIGHT', "4:3",
        """Force the input video to be the given aspect ratio, where WIDTH
        and HEIGHT are integers."""),
    Option('wide', alias=('aspect', '16:9')),
    Option('full', alias=('aspect', '4:3')),
    Option('panavision', alias=('aspect', '235:100')),

    # Other options
    Option('method', 'mpeg2enc|mencoder|ffmpeg', 'ffmpeg',
        """Encode using the given tool. The mpeg2enc method uses mplayer to
        rip the audio and video, and mpeg2enc to encode the video. The
        mencoder and ffmpeg methods do all encoding with the respective
        tool."""),
    Option('fit', 'SIZE', None,
        """Attempt to fit the output into SIZE MiB."""),
    Option('quality', '[1-10]', 8,
        """Desired output quality, on a scale of 1 to 10, with 10 giving
        the best quality at the expense of a larger output file. Output
        size can vary by approximately a factor of 4 (that is, -quality 1
        output can be 25% the size of -quality 10 output). Your results may
        vary."""),
    Option('vbitrate', '[0-9800]', None,
        """Maximum bitrate to use for video (in kbits/sec). Must be within
        allowable limits for the given format. Overrides default values.
        Ignored for VCD."""),
    Option('abitrate', '[0-1536]', None,
        """Encode audio at NUM kilobits per second.  Reasonable values
        include 128, 224, and 384. The default is 224 kbits/sec, good
        enough for most encodings. The value must be within the allowable
        range for the chosen disc format; Ignored for VCD, which must be
        224."""),
    Option('safe', '[0-100]%', "100%",
        """Fit the video within a safe area defined by PERCENT. For
        example, '-safe 90%' will scale the video to 90% of the
        width/height of the output resolution, and pad the edges with a
        black border. Use this if some of the picture is cut off when
        played on your TV."""),
    Option('interlaced', 'top|bottom', None,
        """Do interlaced encoding of the input video. Use this option if
        your video is interlaced, and you want to preserve as much picture
        quality as possible. Ignored for VCD."""),
    Option('deinterlace', '', False,
        """Use this option if your source video is interlaced. You can
        usually tell if you can see a bunch of horizontal lines when you
        pause the video during playback. If you have recorded a video from
        TV or your VCR, it may be interlaced. Use this option to convert to
        progressive (non-interlaced) video. This option is DEPRECATED, and
        will probably be ditched in favor of interlaced encoding, which is
        better in almost every way."""),
    Option('subtitles', 'FILE', None,
        """Get subtitles from FILE and encode them into the video.
        WARNING: This hard-codes the subtitles into the video, and you
        cannot turn them off while viewing the video. By default, no
        subtitles are loaded. If your video is already compliant with the
        chosen output format, it will be re-encoded to include the
        subtitles."""),
    Option('normalize', '', False,
        """Normalize the volume of the audio. Useful if the audio is too
        quiet or too loud, or you want to make volume consistent for a
        bunch of videos."""),
    Option('filters', 'denoise|contrast|deblock[, ...]', [],
        """Apply the given filters to the video before or during
        encoding.""")
]

usage = Usage('pytovid [options] -in FILE -out NAME', *allowed_options)

if __name__ == '__main__':

    options = parse(sys.argv)

    if 'in' not in options or 'out' not in options:
        print(usage)
        print("Please provide an input file (-in) and output name (-out).")
        sys.exit(1)

    encode.encode(options['in'], options['out'], **options)

    print(' ')
    print("Done encoding '%s' to '%s.mpg'" % (options['in'], options['out']))
    print(' ')
    print("Please report bugs to http://code.google.com/p/tovid/issues/list")
    print("Thanks for using pytovid!")

